/**
 * 队列 先进先出 主要为了应对编辑的撤销功能
 * created by Five on 2018-11-7 16:31:28
 * reference http://blog.51cto.com/windeer/1892055
 */

export default class Queue<T> {
    private _element: Array<T>;
    // 默认长度为10
    private _size: number = 10;

    constructor(capacity?: number) {
        this._element = new Array<T>();
        this._size = capacity;
    }

    public push(o: T) {
        if (o == null) {
            return false;
        }
        // 保持队列的长度
        if (this._element.length == this._size) {
            this.pop();
        }
        this._element.unshift(o);
        return true;
    }

    public pop(): T {
        return this._element.pop();
    }

    public size(): number {
        return this._element.length;
    }

    public empty(): boolean {
        return this._size == 0;
    }

    public clear(){
        delete this._element;
        this._element = new Array<T>();
    }
}